package org.java.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

@Configuration
public class CorsConfig {
    // Access-Control-Allow-Origin
    // Access-Control-Allow-Credentials
    // Access-Control-Allow-Methods
    // Access-Control-Max-Age
    // Access-Control-Allow-Headers

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        // 允许跟哪个域名共享访问，*表示所有域名
        configuration.setAllowedOrigins(Arrays.asList("*"));
        // 哪些方式的请求可以共享访问
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS"));
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 哪些URL要考虑跨源访问
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}
